#include "ADUGameManager.h"
#include "ADUConfig.h"

GameManager *GameManager::m_instance = NULL;

GameManager::GameManager() {
	b2Vec2 _gravity;
	_gravity.Set(0.0f, -10.0f);
	mWorld = new b2World(_gravity);
	mWorld->SetAllowSleeping(true);
	mWorld->SetContinuousPhysics(true);
	mWorld->SetContactFilter(&mContactFilter);

}
GameManager* GameManager::getInstance() {
	if(m_instance == NULL) {
		m_instance = new GameManager();
	}
	return m_instance;
}

void GameManager::Step(float _time) {
	int _velocityIterations = 8;
    int _positionIterations = 1;    
    mWorld->Step(_time, _velocityIterations, _positionIterations);


    for (b2Body* b = mWorld->GetBodyList();b;b=b->GetNext()){
    	if (b->GetUserData() != NULL){
    		Sprite* sprt = (Sprite*)b->GetUserData();

    		Vec2 pos = ccp(PTM_RATIO*b->GetPositionX(),PTM_RATIO*b->GetPositionY());
    		sprt->setPosition(pos);
    	}

    }

}
void GameManager::RunScene(int scene) {
	Scene *pScene = NULL;

    switch(scene)
    {
        case GAMEPLAY_SCENE:
            pScene = GamePlayLayer::createScene();
            break;
        default:
            log("Unknown scene");
            break;
    }

	if (Director::getInstance()->getRunningScene() == NULL) {
		Director::getInstance()->runWithScene(pScene);
	} else {
		Scene* newScene = TransitionCrossFade::create(0.5, pScene);
		if(newScene)
			Director::getInstance()->replaceScene(newScene);
	}
}





